// radarPages/radarUserInfo/radarUserInfo.js
import * as echarts from '../ec-canvas/echarts'
import {
  radar_user_info,
  get_ai_record
} from "../../utils/api.js"

let chart=null

const initChart = (canvas, width, height) => {
  chart = echarts.init(canvas, null, {
    width: width,
    height: height
  });
  canvas.setChart(chart);

  // var option = {};

  // chart.setOption(option);
  return chart;
}

const app = getApp()
var page = 1;
var selectedNews = false;
var newsList = [];
const util = require('../../utils/util')
const {
  formatTimeC,
  formatTimeB
} = util

Page({

  /**
   * 页面的初始数据
   */
  data: {
    radarInfo: {},
    SwitchId: 0, // 0：跟进记录，1：AI分析
    ec: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // console.log(options)
    // options.uid = 175
    this.setData({
      options
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    page = 1;
    selectedNews = false;
    newsList = [];
    this.getRadarUserInfo(this.data.options.uid)
    // this.getRadarUserInfo(175)
    let userInfo = app.globalData.userInfo
    this.setData({
      userInfo
    })
  },

  // 获取雷达用户信息 
  getRadarUserInfo: function(uid) {
    const that = this
    app.HttpPostSend(
      radar_user_info, {
        operation_id: app.globalData.userInfo.id,
        uid: uid,
        page: page,
        page_size: 20
      },
      function(res) {
        // console.log(res)
        if (res.msg != "success") {
          app.showTipMsg(res.msg);
        } else {
          if (page == 1) {
            newsList = [];
          }
          let CardUserInfo = JSON.parse(JSON.stringify(res.data.user))
          if (CardUserInfo.username.length>8){
            CardUserInfo.username = CardUserInfo.username.slice(0,8)+'...';
          }
          
          // 1.点赞 2.收藏 3.转发 4. 商城 5.查看 6.聊天窗口
          switch (CardUserInfo.type) {
            case 1:
            CardUserInfo.from = '点赞'
              break;
              case 2:
              CardUserInfo.from = '收藏'
              break;
              case 3:
              CardUserInfo.from = '转发'
              break;
              case 4:
              CardUserInfo.from = '商城'
              break;
              case 5:
              CardUserInfo.from = '查看'
              break;
              case 6:
              CardUserInfo.from = '聊天窗口'
              break;
          
            default:
              break;
          }
          CardUserInfo.add_time = formatTimeB(CardUserInfo.add_time)
          CardUserInfo.last_time = formatTimeB(CardUserInfo.last_time) 

          var radarInfo = res.data.log;
          if (radarInfo.length > 0) {
            const timestamp = parseInt(new Date().getTime() / 1000)
            for (let i = 0; i < radarInfo.length; i++) {
              radarInfo[i].uid = radarInfo[i].uid
              radarInfo[i].username = radarInfo[i].username
              // radarInfo[i].avatar = res.data[i].logo
              switch (radarInfo[i].type) {
                // 1.点赞 2.收藏 3.转发 4. 商城 5.查看 6.聊天窗口
                case 1:
                  radarInfo[i].type = radarInfo[i].type
                  // if (radarInfo[i].status == 0) {
                    radarInfo[i].action = '点赞了'
                    radarInfo[i].able = '你的'
                    radarInfo[i].content = '门店帮扶系统'
                    radarInfo[i].description = ',你的人脉圈很受欢迎'
                  // } else {
                  //   radarInfo[i].action = '取消点赞'
                  //   radarInfo[i].able = '你的'
                  //   radarInfo[i].content = '名片'
                  // }
                  break;

                case 2:
                  radarInfo[i].type = radarInfo[i].type
                  // if (radarInfo[i].status == 0) {
                    radarInfo[i].action = '收藏了'
                    radarInfo[i].able = '你的'
                    radarInfo[i].content = '门店帮扶系统'
                    radarInfo[i].description = ',你的人脉很受欢迎'
                  // } else {
                  //   radarInfo[i].action = '取消收藏'
                  //   radarInfo[i].able = '你的'
                  //   radarInfo[i].content = '名片'
                  // }
                  break;

                case 3:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '转发了'
                  radarInfo[i].able = '你的'
                  radarInfo[i].content = '门店帮扶系统'
                  radarInfo[i].description = ',你的人脉圈正在扩散'
                  break;

                case 4:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '进入了'
                  radarInfo[i].able = '你的'
                  radarInfo[i].content = '商城'
                  break;

                case 5:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '查看了'
                  radarInfo[i].able = '你的'
                  radarInfo[i].content = '门店帮扶系统'
                  // if (radarInfo[i].seeNum > 3) {
                  radarInfo[i].description = '第' + radarInfo[i].seeNum+ '次，强意向客户呦'
                  // }
                  break;

                case 6:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '进入聊天窗口'

                  break;

                case 7:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '访问了'
                  radarInfo[i].able = '你的'
                  radarInfo[i].content = '官网'
                  
                  break;

                case 8:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '查看了'
                  radarInfo[i].able = '你的'
                  radarInfo[i].content = '动态'
                  
                  break;

                case 9:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '播放了'
                  radarInfo[i].able = '你的'
                  radarInfo[i].content = '视频'
                  
                  break;

                case 10:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '播放了'
                  radarInfo[i].able = '你的'
                  radarInfo[i].content = '语音'
                  
                  break;

                case 11:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '访问了'
                  radarInfo[i].able = '你的'
                  radarInfo[i].content = '商城'
                  
                  break;

                case 12:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '查看了'
                  radarInfo[i].able = '你的'
                  radarInfo[i].content = '广场分类'
                  
                  break;

                case 13:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '浏览了'
                  radarInfo[i].able = '你的名为'
                  if (radarInfo[i].goodsName.length > 10) {
                    var goodsName = '"' + radarInfo[i].goodsName.slice(0, 10) + '...' + '"'
                  } else if (radarInfo[i].goodsName.length == 0) {
                    goodsName = ''
                  } else {
                    goodsName = '"' + radarInfo[i].goodsName + '"'
                  }
                  radarInfo[i].content = goodsName
                  radarInfo[i].able2 = '的商品'
                  
                  break;

                case 14:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '进入了'
                  radarInfo[i].able = ''
                  radarInfo[i].content = '购买尽猎AI门店帮扶系统页面'
                  
                  break;

                case 15:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '点击付款了'
                  radarInfo[i].able = '你的'
                  radarInfo[i].content = '尽猎AI门店帮扶系统'

                  break;

                case 16:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '成功购买了'
                  radarInfo[i].able = '你的'
                  radarInfo[i].content = '尽猎AI门店帮扶系统'

                  break;

                case 17:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '添加了'
                  radarInfo[i].able = '新的'
                  radarInfo[i].content = '地址'

                  break;

                case 18:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '提交了'
                  radarInfo[i].able = '新的'
                  radarInfo[i].content = '商品订单'

                  break;

                case 19:
                  if (radarInfo[i].goodsName == null) {
                    radarInfo[i].content = '商城支付了订单'
                  } else {
                    radarInfo[i].content = '商城支付了订单(订单金额：' + radarInfo[i].goodsName +')'
                  }
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '在'
                  radarInfo[i].able = '你的'
                  // radarInfo[i].content = '商城支付了订单'

                  break;

                case 20:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '取消了'
                  radarInfo[i].able = '你的'
                  radarInfo[i].content = '商品订单'

                  break;

                case 21:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '点赞了'
                  radarInfo[i].able = '你的'
                  radarInfo[i].content = '动态'

                  break;
                case 22:
                  if (radarInfo[i].goodsName == null) {
                    radarInfo[i].content = '进店扫码支付了订单'
                  } else {
                    radarInfo[i].content = '进店扫码支付了订单(订单金额：' + radarInfo[i].goodsName + ')'
                  }
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '在'
                  radarInfo[i].able = '你的'

                  break;
                case 30:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '保存了'
                  radarInfo[i].able = '你的'
                  radarInfo[i].content = '人脉海报'

                  break;
                case 31:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '添加'
                  radarInfo[i].able = '商品'
                  if (radarInfo[i].goodsName.length > 10) {
                    var goodsName = '"' + radarInfo[i].goodsName.slice(0, 10) + '...' + '"'
                  } else if (radarInfo[i].goodsName.length == 0) {
                    goodsName = ''
                  } else {
                    goodsName = '"' + radarInfo[i].goodsName + '"'
                  }
                  radarInfo[i].content = goodsName
                  radarInfo[i].able2 = '到购物车'
                  // if (radarInfo[i].num > 3) {
                  //   radarInfo[i].description = '第' + radarInfo[i].num + '次，强意向客户呦'
                  // }
                  break;

                case 32:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '分享了'
                  radarInfo[i].able = '商品'
                  if (radarInfo[i].goodsName.length > 10) {
                    var goodsName = '"' + radarInfo[i].goodsName.slice(0, 10) + '...' + '"'
                  } else if (radarInfo[i].goodsName.length == 0) {
                    goodsName = ''
                  } else {
                    goodsName = '"' + radarInfo[i].goodsName + '"'
                  }
                  radarInfo[i].content = goodsName
                  radarInfo[i].able2 = '的链接'
                  // if (radarInfo[i].num > 3) {
                  //   radarInfo[i].description = '第' + radarInfo[i].num + '次，强意向客户呦'
                  // }
                  break;
                case 33:
                  radarInfo[i].type = radarInfo[i].type
                  radarInfo[i].action = '保存了'
                  radarInfo[i].able = '商品'
                  if (radarInfo[i].goodsName.length > 10) {
                    var goodsName = '"' + radarInfo[i].goodsName.slice(0, 10) + '...' + '"'
                  } else if (radarInfo[i].goodsName.length == 0) {
                    goodsName = ''
                  } else {
                    goodsName = '"' + radarInfo[i].goodsName + '"'
                  }
                  radarInfo[i].content = goodsName
                  radarInfo[i].able2 = '的海报'
                  // if (radarInfo[i].num > 3) {
                  //   radarInfo[i].description = '第' + radarInfo[i].num + '次，强意向客户呦'
                  // }
                  break;

                default:
                  break;
              }
              let aim_timestamp = parseInt(new Date(radarInfo[i].add_time * 1000).getTime() / 1000)
              radarInfo[i].add_time = formatTimeC(timestamp, aim_timestamp)

              newsList.push(radarInfo[i]);
            }
          }

          if (radarInfo.length < 20) {
            selectedNews = false;
          } else {
            selectedNews = true;
            page += 1;
          }

          that.setData({
            CardUserInfo,
            radarInfo: newsList
          })

          wx.hideLoading();
        }
      })
  },

  showCarte: function(e) {
    const that = this
    console.log(e)
    app.globalData.SwitchTabParams.v_id = e.currentTarget.dataset.carte_id
    wx.navigateTo({
      url: '/pages/carteShow/carteShow',
    })
  },

  //点击进入聊天页面
  showContactMessage: function (e) {
    const that = this
    var id = e.currentTarget.dataset.id;
    
    wx.navigateTo({
      url: '/pages/socks/socks?mid=' + e.currentTarget.dataset.id
    })
    // var id = e.currentTarget.dataset.id;
    // wx.navigateTo({
    //   url: '/pages/contactUser/contactUser?id=' + id
    // })
  },

  // ai 纪录 切换
  recordSwitch: function(e) {
    const that = this
    page = 1;
    selectedNews = false;
    newsList = [];
    const tap_switch_id = e.currentTarget.dataset.id
    const SwitchId = this.data.SwitchId
    if (tap_switch_id == SwitchId) {
      // console.log(true)
    } else {
      // console.log(SwitchId,tap_switch_id)
      let change = new Object()
      switch (tap_switch_id) {
        case 0:
          change['SwitchId'] = tap_switch_id
          that.getRadarUserInfo(that.data.options.uid)
          break;

        case 1:
          change['SwitchId'] = tap_switch_id
          that.getAiRecord(that.data.userInfo.id, that.data.options.uid)
          break;

        default:
          break;
      }
      that.setData(change)
    }
  },

  // 获取雷达信息 
  getAiRecord: function (uid, user_id ) {
    const that = this
    try {
      app.HttpGetSend(
        get_ai_record, {
          // operation_id: app.globalData.userInfo.id,
          operation_id: uid,
          user_id
        },
        function(res) {
          // {
          //   "msg": "success",
          //   "data": {
          //     "loveMe": 2,                // 总数
          //     "loveMePercent": 67,        // 百分比
          //     "loveGoods": 1,             // 总数
          //     "loveGoodsPercent": 33,     // 百分比
          //     "loveCompany": 0,           // 总数
          //     "loveCompanyPercent": 0     // 百分比
          //   },
          //   "code": 1
          // }
          // console.log(res)
          if (res.msg != "success") {
            app.showTipMsg(res.msg);
          } else {
            let AiRecord = new Object()
            AiRecord.type = 0
            if (JSON.stringify(res.data) != '{}' && res.data != null) {
              AiRecord.type = 1
              AiRecord.list = new Array()
              let seriesData = [
                {
                  value: res.data.loveCompany,
                  name: '对公司感兴趣'
                }, {
                  value: res.data.loveGoods,
                  name: '对商品感兴趣',
                }, {
                  value: res.data.loveMe,
                  name: '对我感兴趣'
                }
              ]
              let options = {
                title : {
                  text: '客户兴趣占比',
                  textStyle: {
                    fontSize: 12
                  },
                  top: 10
                },
                backgroundColor: "#ffffff",
                // color: ["#37A2DA", "#32C5E9", "#67E0E3", "#91F2DE", "#FFDB5C", "#FF9F7F"],
                tooltip : {
                  trigger: 'name'
                },
                legend: {
                  type: 'scroll',
                  orient: 'horizontal',
                  right: 10,
                  bottom: 20,
                },
                series: [{
                  name: '客户兴趣占比',
                  label: {
                    normal: {
                      fontSize: 14
                    }
                  },
                  type: 'pie',
                  center: ['50%', '50%'],
                  radius: '60%',
                  data: seriesData,
                  label: {
                    show: true,
                    formatter: `{d}%`,
                  },
                  itemStyle: {
                    emphasis: {
                      shadowBlur: 10,
                      shadowOffsetX: 0,
                      shadowColor: 'rgba(0, 2, 2, 0.3)'
                    }
                  }
                }]
              }
              chart.setOption(options)
            }
            // that.canvasInit()
            that.setData({
              AiRecord
            })
          }
        })
    } catch (error) {

    }
  },

  echartChange: function (data) {
    chart.setOption(data)
  },

  echartInit: function (e) {
    // console.log(e)
    initChart(e.detail.canvas, e.detail.width, e.detail.height)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (selectedNews == true) {
      var that = this;
      // 显示加载图标  
      wx.showLoading({
        title: '玩命加载中',
      })
      // 页数+1  
      that.getRadarUserInfo(that.data.options.uid)

    } else {
      wx.hideLoading();
    }
  },
// 拨打电话
  makeCall: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.content // 仅为示例，并非真实的电话号码
    })
  },
  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function() {

  // }
})